package com.android.androidbigwork.activity;

import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import androidx.appcompat.app.AppCompatActivity;
import com.android.androidbigwork.MainActivity;
import com.android.androidbigwork.R;
import com.android.androidbigwork.entity.User;
import com.android.androidbigwork.pojo.LoginUser;
import com.android.androidbigwork.utils.RequestUtils;
import com.google.gson.Gson;

import java.io.IOException;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class LoginActivity extends AppCompatActivity {
    private static String URL = "http://39.108.183.17:8080/login" ;

    private EditText accountEditText;
    private EditText passwordEditText;
    private Button submitButton;
    private TextView tip;
    private TextView registry;



    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.login);

        accountEditText = findViewById(R.id.account);
        passwordEditText = findViewById(R.id.password);
        submitButton = findViewById(R.id.submit);
        tip = findViewById(R.id.login_tip);
        registry = findViewById(R.id.click_to_registry);

        submitButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                final String account = accountEditText.getText().toString();
                Pattern pattern = Pattern.compile("[1-9]+[0-9]*");
                Matcher matcher = pattern.matcher(account);
                if(matcher.matches()){
                    String password = passwordEditText.getText().toString();
                    User user =new User();
                    user.setId(Integer.parseInt(account));
                    user.setPassword(password);
                    Log.d("输入信息", user.toString());
                    final String json = new Gson().toJson(user);

                    new Thread(new Runnable() {
                        @Override
                        public void run() {
                            try {
                                Log.d("", "run: 进入run");
                                String result =  RequestUtils.post(URL,json);
                                Log.d("点击登录", result);
                                if(result.equals("登录成功"))
                                {
                                    LoginUser.setAccount(Integer.valueOf(accountEditText.getText().toString()));
                                    Intent intent = new Intent(LoginActivity.this, MainActivity.class);
                                     startActivity(intent);
                                }
                                else{
                                        tip.setText("账户或密码错误！");
                                }
                            }catch (IOException e){
                                e.printStackTrace();
                            }

                        }
                    }).start();



                }
            }
        });

        registry.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(LoginActivity.this,RegistryActivity.class);
                startActivity(intent);
            }
        });

    }
}
